﻿using Orderr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderForm
{
    public partial class Form1 : Form
    {
        public OrderService master;
        public Form1()
        {
            InitializeComponent();
            Good good1 = new Good("banana", 25);
            Good good2 = new Good("apple", 20);
            Good good3 = new Good("milk", 10);
            Client client1 = new Client("邢博");
            Client client2 = new Client("周冬");
            Client client3 = new Client("张瑞铮");
            Order list1 = new Order(114, client1, new List<OrderDetails>());
            list1.Details.Add(new OrderDetails(good1, 3));
            list1.Details.Add(new OrderDetails(good2, 4));
            Order list2 = new Order(514, client2, new List<OrderDetails>());
            list2.Details.Add(new OrderDetails(good1, 3));
            list2.Details.Add(new OrderDetails(good3, 5));
            Order list3 = new Order(191, client3, new List<OrderDetails>());
            list3.Details.Add(new OrderDetails(good1, 2));
            list3.Details.Add(new OrderDetails(good3, 4));
            list3.Details.Add(new OrderDetails(good2, 5));

            master = new OrderService();
            master.add(list1);
            master.add(list2);
            master.add(list3);
            OrderGrid.DataSource = master.Orders;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int oid;
            try { oid = int.Parse(textBox1.Text); OrderGrid.DataSource = master.QueryByOid(oid); }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int price;
            try { price = int.Parse(textBox2.Text); OrderGrid.DataSource = master.QueryByPrice(price); }
            catch { }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OrderGrid.DataSource = null;
            OrderGrid.DataSource = master.Orders;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String name = textBox3.Text;
            try { OrderGrid.DataSource = master.QueryByGood(new Good(name, 0)); }
            catch { }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var rows = OrderGrid.SelectedRows;
            OrderService temp = master;
            if (rows.Count > 0)
            {
                foreach (DataGridViewRow row in rows)
                {
                    int oid = Convert.ToInt32(row.Cells["oidd"].Value);
                    temp.delete(oid);
                    OrderGrid.DataSource = null;
                    OrderGrid.DataSource = master.Orders;
                }
                master = temp;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show(this);
        }
    }
}
